//
//  DestinationSearchViewController.m
//  结伴
//
//  Created by eliot on 16/8/10.
//  Copyright © 2016年 aleven. All rights reserved.
//

#import "DestinationSearchViewController.h"

@interface DestinationSearchViewController ()<UISearchBarDelegate>

@property (nonatomic, strong) UISearchBar *searchBar;


@end

@implementation DestinationSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpHotCity];
    self.view.backgroundColor = eceColor;
    [self setTitleView];
    
}

/** 设置导航栏*/
- (void)setTitleView {
    
    
    _searchBar = [[UISearchBar alloc]initWithFrame:self.navigationController.navigationBar.frame];
    _searchBar.delegate = self;
    _searchBar.keyboardType = UIKeyboardAppearanceDefault;
    _searchBar.placeholder = @"请输入目的地";
    _searchBar.delegate = self;
    _searchBar.searchBarStyle = UISearchBarStyleDefault;
    _searchBar.barStyle = UIBarStyleDefault;
    [_searchBar setSearchResultsButtonSelected:YES];
    [_searchBar setClipsToBounds:YES];
    _searchBar.tintColor=[UIColor blueColor];
    self.navigationItem.titleView = _searchBar;
}
/** 设置热门城市*/
- (void)setUpHotCity {
    
    
    for (UIView *subView in self.view.subviews) {
        [subView removeFromSuperview];
    }
    
    
    UIView *bgv = [[UIView alloc] initWithFrame:CGRectMake(0, 74, Swidth, Sheight - 10)];
    NSArray *cityArr = @[@"广州",@"南昌",@"广州",@"南昌",@"广州",@"南昌",@"广州",@"南昌",@"广州",@"南昌",@"广州",@"南昌"];
    [self.view addSubview:bgv];
    bgv.backgroundColor = [UIColor whiteColor];
    
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 15, Swidth - 30, 21)];
    label.text = @"热门城市";
    label.font = [UIFont systemFontOfSize:19];
    [bgv addSubview:label];
    label.textColor = [UIColor redColor];
    
    
    UIView *lastView = nil;
    for (int i = 0; i < 12; i ++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i;
        
        [bgv addSubview:btn];
        
        [btn setTitle:cityArr[i] forState:UIControlStateNormal];
        
        btn.layer.cornerRadius = 12;
        
        btn.layer.masksToBounds = YES;
        
        [btn addTarget:self action:@selector(clickAmountOfChargeBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        btn.backgroundColor =  bbeColor
        
        CGFloat Width = (Swidth - 100) / 4.0;
        
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.width.mas_equalTo(Width);
            
            make.height.mas_equalTo(30);
            

            if (i % 4 == 0) {
                make.left.mas_equalTo(20);
                if (i / 4 == 0) {
                    make.top.mas_equalTo(51);
                }else {
                    make.top.mas_equalTo(51 + 45*(i / 4));
                }
            }
            if (i % 4 == 1) {
                make.left.mas_equalTo((Swidth - 100) / 4 + 40);
                if (i / 4 == 0) {
                    make.top.mas_equalTo(51);
                }else {
                    make.top.mas_equalTo(51 + 45*(i / 4));
                }
            }
            if (i % 4 == 2) {
                make.left.mas_equalTo((Swidth - 100) / 2 + 60);
                if (i / 4 == 0) {
                    make.top.mas_equalTo(51);
                }else {
                    make.top.mas_equalTo(51 + 45*(i / 4));
                }
            }
            if (i % 4 == 3) {
                make.left.mas_equalTo((Swidth - 100) / 4 * 3 + 80);
                if (i / 4 == 0) {
                    make.top.mas_equalTo(51);
                }else {
                    make.top.mas_equalTo(51 + 45*(i / 4));
                }
            }
        }];
        lastView = btn;
       
    }
    
    
    UILabel *lineLb = [[UILabel alloc] init];
    [bgv addSubview:lineLb];
    [lineLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(lastView.mas_bottom).mas_equalTo(15);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(1);
    }];
    lineLb.backgroundColor = eceColor;
    
    
    
    
    UILabel *label2 = [[UILabel alloc] init];
    [bgv addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(lineLb.mas_bottom).mas_equalTo(15);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(21);
        
    }];
    label2.text = @"历史搜索";
    label2.font = [UIFont systemFontOfSize:19];
    [bgv addSubview:label2];
    label2.textColor = Five0cColor;
    
    NSArray *searchHistoryArr = [[NSUserDefaults standardUserDefaults] objectForKey:@"searchHistoryArr"];

    if (searchHistoryArr.count == 0) {
        return;
    }
    
    for (int i = 0; i < searchHistoryArr.count; i ++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i;
        [bgv addSubview:btn];
        [btn setTitle:searchHistoryArr[i] forState:UIControlStateNormal];
        btn.layer.cornerRadius = 12;
        btn.layer.masksToBounds = YES;
        [btn addTarget:self action:@selector(clickAmountOfChargeBtn:) forControlEvents:UIControlEventTouchUpInside];
        btn.backgroundColor =  bbeColor
        CGFloat Width = (Swidth - 100) / 4.0;
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(Width);
            make.height.mas_equalTo(30);
            
            
            if (i % 4 == 0) {
                make.left.mas_equalTo(20);
                if (i / 4 == 0) {
                    make.top.mas_equalTo(label2.mas_bottom).mas_equalTo(15);
                }else {
                    make.top.mas_equalTo(label2.mas_bottom).mas_equalTo(15 + 45*(i / 4));
                }
            }
            if (i % 4 == 1) {
                make.left.mas_equalTo((Swidth - 100) / 4 + 40);
                if (i / 4 == 0) {
                    make.top.mas_equalTo(label2.mas_bottom).mas_equalTo(15);
                }else {
                    make.top.mas_equalTo(label2.mas_bottom).mas_equalTo(15 + 45*(i / 4));
                }
            }
            if (i % 4 == 2) {
                make.left.mas_equalTo((Swidth - 100) / 2 + 60);
                if (i / 4 == 0) {
                    make.top.mas_equalTo(label2.mas_bottom).mas_equalTo(15);
                }else {
                    make.top.mas_equalTo(label2.mas_bottom).mas_equalTo(15 + 45*(i / 4));
                }
            }
            if (i % 4 == 3) {
                make.left.mas_equalTo((Swidth - 100) / 4 * 3 + 80);
                if (i / 4 == 0) {
                    make.top.mas_equalTo(label2.mas_bottom).mas_equalTo(15);
                }else {
                    make.top.mas_equalTo(label2.mas_bottom).mas_equalTo(15 + 45*(i / 4));
                }
            }
        }];
        
    }

}


/** 点击热门城市*/
- (void)clickAmountOfChargeBtn:(UIButton *)btn {

    [self.searchBar resignFirstResponder];
    NSArray *aa = [[NSUserDefaults standardUserDefaults] objectForKey:@"searchHistoryArr"];
    if (aa) {
        
        NSMutableArray *searchHistoryArr = [NSMutableArray arrayWithArray:aa];
        [searchHistoryArr addObject:btn.currentTitle];
        if ([searchHistoryArr containsObject:btn.currentTitle]) {
            [searchHistoryArr removeObject:btn.currentTitle];
            [searchHistoryArr insertObject:btn.currentTitle atIndex:0];
        }
        [[NSUserDefaults standardUserDefaults] setObject:[searchHistoryArr copy] forKey:@"searchHistoryArr"];
        
    }else {
        NSArray *aa = @[btn.currentTitle];
        [[NSUserDefaults standardUserDefaults] setObject:aa forKey:@"searchHistoryArr"];
    }
    
    [self setUpHotCity];
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    [self.searchBar resignFirstResponder];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    NSArray *aa = [[NSUserDefaults standardUserDefaults] objectForKey:@"searchHistoryArr"];
    if (searchBar.text.length > 0) {
        
        if (aa) {
            
            NSMutableArray *searchHistoryArr = [NSMutableArray arrayWithArray:aa];
            [searchHistoryArr addObject:searchBar.text];
            if ([searchHistoryArr containsObject:searchBar.text]) {
                [searchHistoryArr removeObject:searchBar.text];
                [searchHistoryArr insertObject:searchBar.text atIndex:0];
            }
            [[NSUserDefaults standardUserDefaults] setObject:[searchHistoryArr copy] forKey:@"searchHistoryArr"];
            
        }else {
            NSArray *aa = @[searchBar.text];
            [[NSUserDefaults standardUserDefaults] setObject:aa forKey:@"searchHistoryArr"];
        }
        
        
    }else {
    
    
    }
     [self setUpHotCity];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
